(function($, window) {
	
	var _top = window !== top ? top : window,
	defaults = {
		title : "提示",
		resizable : true,
		modal : true,
		width : 800,
		height : 600,
		draggable : true,
		position: { my : "center", at : "center", of : _top, collision: "none" },
		autoopen : true,
		close : function() {
			$(this).find("iframe").remove();
			$(this).remove();
			//this.parentNode.removeChild(this); 
		},
		relay : function() { // 数据传递回调
			//console.info(arguments);
		}
	};
	
	
	$.extend({
		openUrl : function(url, options) {
			var dialogId = "uuid_" + UUID(),
			dialogDiv = $("<div>").attr("id", dialogId),
			iframe = $("<iframe width='100%' height='99%' frameborder='no' border='0' />"),
			cfg = $.extend({}, defaults, options);
			
			
			
			iframe.bind("load", function() {
				if (_top.$("#loading").length != 0) _top.$("#loading").hideLoading();
				this.contentWindow.opener = window;
				this.contentWindow.opener.closeSelf = function() {
					dialogDiv.find("iframe").remove(); // 防止页面元素放回body的再次加载
					dialogDiv.dialog("close");
				};
				this.contentWindow.opener.relay = cfg.relay;
			});
			dialogDiv.appendTo(_top.document.body);
			dialogDiv.append(iframe);
			dialogDiv.dialog(cfg);
			iframe.attr("src", url);
			if (_top.$("#loading").length != 0) _top.$("#loading").showLoading();
		}
	});
	
	$.fn.extend({
		setInputName4Inject : function() {
			setInputName4Inject(this);
		},
		resetInputName4Inject : function() {
			resetInputName4Inject(this);
		}
	});
	
	function setInputName4Inject(selector) {
		$(selector).each(function(index) {
			$(":input[rule]", this).each(function() {
				var $this = $(this),
				rule = $this.attr("rule"),
				array = rule.split("_"),
				obj = array[0],
				pro = array[1],
				originalName = $this.attr("name");
				
				$this.attr("originalName", originalName);
				$this.attr("name", obj + "[" + index + "]." + pro);
			});
		});
	}

	function resetInputName4Inject(selector) {
		$(selector).each(function(index) {
			$(":input[rule]", this).each(function() {
				var $this = $(this);
				$this.attr("name", $this.attr("originalName"));
			});
		});
	}
	
	function UUID() {
		return new Date().getTime();
	}
	
})(jQuery, window);

/*
IE Doesn't have a .startsWith either?
*/
if (!String.prototype.startsWith) {
	String.prototype.startsWith = function (str){
	  return this.lastIndexOf(str, 0) === 0;
	};
}